<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  
  <script>
    var obj = {
      a: 10
    }
    /* 
    Object.defineProperty(对象,'属性',{
      get(){},
      set(){}
    })
    */
   var val = 100;
   Object.defineProperty(obj, 'a', {
     get(){ // getter 劫持了 obj的a属性获取值得操作
      return val;
     },
     set(newVal){ // setter 劫持了obj的a属性设置值得操作
      console.log('setter触发了');
      val = newVal;
     }
   })

   console.log(obj.a);
  
   obj.a = 500;
   console.log(obj.a);
  </script>
</body>
</html>